<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Models\ReceivingAddres;
use EasyWeChat\Factory;
use EasyWeChat\Foundation\Application;
use function EasyWeChat\Kernel\Support\generate_sign;

class ReceivingAddresController extends Controller
{

  public function add(Request $request)
  {
        $receivingAddres = new ReceivingAddres();
        $code = $request->input('jsCode');
        $miniProgram = \EasyWeChat::miniProgram();
        $app = $miniProgram->auth->session($code);
        // dd($app);
        $openid = $app['openid'];
        // dd($openid);
        // $openid = $request->input('openid');
        $user = User::where('openid', $openid)->get()->toArray();
        $user_id =$user[0]['id'];
        // $user_id = 1;
        $name = $request->input('name');
        $phone_number = $request->input('phone_number');
        $email = $request->input('email');

        $region = $request->input('region');
        $detailed_address = $request->input('detailed_address');

        $receivingAddres->name = $name;
        $receivingAddres->phone_number = $phone_number;
        // $receivingAddres->email = $email;
        $receivingAddres->region = $region;
        $receivingAddres->detailed_address = $detailed_address;
        $receivingAddres->user_id = $user_id;
        $result = $receivingAddres->save();
  }

  public function list(Request $request)
  {
    // return 0;
    $code = $request->input('jsCode');
    $miniProgram = \EasyWeChat::miniProgram();
    $app = $miniProgram->auth->session($code);
    $openid = $app['openid'];
    // $openid = $request->input('openid');
    // return $request->all();
    $user = User::where('openid', $openid)->get()->toArray();
    $user_id =$user[0]['id'];
    // return $user_id;
    $user = User::find($user_id);
    $data = $user->receivingAddress()->paginate(10);
    return $data;
  }

  public function edit(Request $request)
  {
    $name = $request->input('name');
    $phone_number = $request->input('phone_number');

    $region = $request->input('region');
    $detailed_address = $request->input('detailed_address');

     $receivingAddress_id = $request->input("id");
     $receivingAddress = ReceivingAddres::find($receivingAddress_id);
     $receivingAddress->name = $name;
     $receivingAddress->phone_number = $phone_number;
     $receivingAddress->region = $region;
     $receivingAddress->detailed_address = $detailed_address;
     if($receivingAddress->save()){
       return response()->json([
                 'code' => 200,
                 'msg'=>'修改地址成功!',
             ], 200);
     }
  }
  // php artisan make:resource Commodity
  // php artisan make:resource CaseCenter
  // php artisan make:resource CaseCenterCollection
  //收货地址删除
  public function delete(Request $request)
  {
    $receivingAddres_id = $request->input("id");
    ReceivingAddres::destroy($receivingAddres_id);
  }

  //收货地址详情
  public function detail(Request $request)
  {
      $receivingAddres_id = $request->input("id");
      $data =  ReceivingAddres::find($receivingAddres_id);
      if(count($data)<1){
        return response()->json([
                  'msg' => 'Not Found！',
              ], 200);
      }else {
        return $data;
      }
  }

}
